import { Component, OnInit, Input } from '@angular/core';
import { AppGlobalService } from '../../shared/service/app-global.service';

@Component({
    selector: 'app-brand',
    templateUrl: './brand.component.html',
    styleUrls: ['./brand.component.scss'],
})
export class BrandComponent implements OnInit {

    @Input() tagData: any;
    brandData = [];
    defaultImg = 'assets/img/default_img.png';
    constructor(
        private appGlobalService: AppGlobalService
    ) {
    }

    ngOnInit() {
        const len = this.tagData.data.data.length;
        if (len > 0) {
            for (let i = 0; i < len; i++) {
                const v = this.tagData.data.data[i];
                if (v.imageUrl === undefined || v.imageUrl === "") {

                    v.imageUrl = this.defaultImg;
                }
                this.brandData.push(v);
            }
        }
    }

    go(moreButton?) {
        if (moreButton.link === undefined || (moreButton.link !== undefined && moreButton.link === '')) {
            return;
        }
        const sid = localStorage.getItem('sessionId');
        const targetObj = {
            pageId: this.tagData.data.moreButton.link.target,
            params: this.tagData.data.moreButton.link.params,
            sessionId: sid
        };
        this.appGlobalService.goToForward(targetObj);
    }

    getNgStyle(str) {
        try {
            return JSON.parse(str);
        } catch (error) {
            return {};
        }
    }

}
